package org.dxy.infrastructure.persistent.db.mybatispuls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.dxy.infrastructure.persistent.db.mybatispuls.po.ProcessParamRequirementPo;
import org.dxy.infrastructure.persistent.db.mybatispuls.mapper.ProcessParamRequirementMapper;
import org.dxy.infrastructure.persistent.db.mybatispuls.service.ProcessParamRequirementService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 工序参数配置Service实现类
 * @author: dxy
 * @createTime: 2025/1/21
 */
@Service
public class ProcessParamRequirementServiceImpl extends MPJBaseServiceImpl<ProcessParamRequirementMapper, ProcessParamRequirementPo> implements ProcessParamRequirementService {

    @Override
    public Page<ProcessParamRequirementPo> selectPageListWithDetails(Page<ProcessParamRequirementPo> page, ProcessParamRequirementPo query) {
        LambdaQueryWrapper<ProcessParamRequirementPo> wrapper = new LambdaQueryWrapper<>();
        
        if (query.getProcessId() != null) {
            wrapper.eq(ProcessParamRequirementPo::getProcessId, query.getProcessId());
        }
        if (query.getParamDefinitionId() != null) {
            wrapper.eq(ProcessParamRequirementPo::getParamDefinitionId, query.getParamDefinitionId());
        }
        if (query.getRequired() != null) {
            wrapper.eq(ProcessParamRequirementPo::getRequired, query.getRequired());
        }
        
        wrapper.orderByDesc(ProcessParamRequirementPo::getCreateTime);
        
        // 注意：这里只能查询基础字段，关联字段需要通过JOIN查询或者后续填充
        // 在实际项目中，可以使用MPJ的selectJoinPage方法进行关联查询
        return this.page(page, wrapper);
    }

    @Override
    public List<ProcessParamRequirementPo> selectByProcessIdWithDetails(Long processId) {
        LambdaQueryWrapper<ProcessParamRequirementPo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessParamRequirementPo::getProcessId, processId);
        wrapper.orderByDesc(ProcessParamRequirementPo::getRequired)
               .orderByAsc(ProcessParamRequirementPo::getId);
        
        // 注意：这里只能查询基础字段，关联字段需要通过JOIN查询或者后续填充
        return this.list(wrapper);
    }

    @Override
    public List<ProcessParamRequirementPo> selectByParamDefinitionIdWithDetails(Long paramDefinitionId) {
        LambdaQueryWrapper<ProcessParamRequirementPo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessParamRequirementPo::getParamDefinitionId, paramDefinitionId);
        wrapper.orderByAsc(ProcessParamRequirementPo::getProcessId);
        
        // 注意：这里只能查询基础字段，关联字段需要通过JOIN查询或者后续填充
        return this.list(wrapper);
    }

    @Override
    public List<ProcessParamRequirementPo> selectRequiredParamsWithDetails(Long processId) {
        LambdaQueryWrapper<ProcessParamRequirementPo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessParamRequirementPo::getRequired, true);
        if (processId != null) {
            wrapper.eq(ProcessParamRequirementPo::getProcessId, processId);
        }
        wrapper.orderByAsc(ProcessParamRequirementPo::getProcessId)
               .orderByAsc(ProcessParamRequirementPo::getId);
        
        // 注意：这里只能查询基础字段，关联字段需要通过JOIN查询或者后续填充
        return this.list(wrapper);
    }

    @Override
    public ProcessParamRequirementPo selectByIdWithDetails(Long id) {
        // 注意：这里只能查询基础字段，关联字段需要通过JOIN查询或者后续填充
        return this.getById(id);
    }

    @Override
    public int checkProcessParamExists(Long processId, Long paramDefinitionId, Long excludeId) {
        LambdaQueryWrapper<ProcessParamRequirementPo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessParamRequirementPo::getProcessId, processId);
        wrapper.eq(ProcessParamRequirementPo::getParamDefinitionId, paramDefinitionId);
        if (excludeId != null) {
            wrapper.ne(ProcessParamRequirementPo::getId, excludeId);
        }
        return Math.toIntExact(this.count(wrapper));
    }

    @Override
    public int deleteByProcessId(Long processId) {
        LambdaQueryWrapper<ProcessParamRequirementPo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessParamRequirementPo::getProcessId, processId);
        return Math.toIntExact(this.remove(wrapper) ? this.count(wrapper) : 0);
    }

    @Override
    public int batchDeleteByIds(List<Long> ids) {
        // 使用逻辑删除
        return Math.toIntExact(this.removeByIds(ids) ? ids.size() : 0);
    }

    @Override
    public int batchInsert(List<ProcessParamRequirementPo> entities) {
        // 批量保存
        return this.saveBatch(entities) ? entities.size() : 0;
    }
}